/******************************************************************************
 *      Mestrado em Ciência da Computação - UFMS
 *		Redes de Computadores - Prof. Dr. Ronaldo Alves Ferreira
 *		Alunos: Geraldo Landre, Marcel Tolentino, Vinícius Bueno
 *****************************************************************************/

#ifndef MY_SOCKET_H
#define	MY_SOCKET_H

#include <sys/types.h>
#include <sys/socket.h>

#include <stdio.h>

#include "defines.h"
#include "thread.h"
#include "tcpfms.h"
#include "tcb.h"
#include "tcbtab.h"

extern int tcpserver(struct tcb *ptcb, u_short lport);
int tcpbind(struct tcb *ptcb, ipaddr fip, u_short fport);
int tcpcon(struct tcb *);
int tcpwrite(int tcbnum, unsigned char *pch, unsigned len);
int tcpread(int tcbnum, char *pch, unsigned len);

/**
 * Funcao SOCKET adaptada para TCP sobre UDP
 * Ver:
 *      docs/socket.txt
 * @param domain
 * @param type
 * @param protocol
 * @return File Descriptor em caso de sucesso ou -1 em caso de erro
 */
int my_socket(int domain, int type, int protocol);

/**
 * Funcao LISTEN adaptada para o TCP sobre UDP
 * Ver:
 *      docs/listen.txt
 * @param sockfd
 * @param backlog
 * @return 0 on success, -1 for errors.
 */
int my_listen(int sockfd, int backlog);

/**
 * Funcao BIND adaptada para TCP sobre UDP
 * Ver:
 *      docs/bind.txt
 * @param sockfd
 * @param addr
 * @param addrlen
 * @return On success, zero is returned.  On error, -1 is returned, and errno is set appropriately.
 */
int my_bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

/**
 * Funcao ACCEPT adaptada para o TCP sobre UDP
 * Ver:
 *      docs/accept.txt
 * @param sockfd
 * @param addr
 * @param addrlen
 * @return the new socket's descriptor, or -1 for errors.
 */
int my_accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

/**
 * Funcao RECV adaptada para TCP sobre UDP
 * Ver:
 *      docs/recv.txt
 *      docs/recvfrom.txt
 * @param fd file descriptor
 * @param buf buffer para receber os arquivos
 * @param n tamanho do buffer
 * @param flags
 * @return numero de bytes lidos em caso de sucesso, -1 em caso de erro
 */
ssize_t my_recv(int fd, void *buf, size_t n, int flags);

/**
 * Funcao SEND adaptada para TCP sobre UDP
 * Ver:
 *      docs/send.txt
 *      docs/sendto.txt
 * @param sockfd
 * @param buf
 * @param len
 * @param flags
 * @return On success, these calls return the number of characters sent.  On error, -1 is returned, and errno is set appropriately.
 */
ssize_t my_send(int sockfd, const void *buf, size_t len, int flags);

/**
 * Funcao CONNECT adaptada para TCP sobre UDP
 * Ver:
 *      docs/connect.txt
 * @param sockfd
 * @param addr
 * @param addrlen
 * @return 0 on success, -1 for errors.
 */
int my_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

int my_close(int sockfd);

void init_tcp(u_short my_port, int length);

void *input_function();
void *output_function();
void *timer_function();
void *udprecver_function(void *param);

int tcp_in(struct udp *pudp);

#endif	/* MY_SOCKET_H */

